package com.ruoyi.web.controller.community.integral;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.IntApply;
import com.ruoyi.community.service.IIntApplyService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.community.domain.IntSet;
import com.ruoyi.community.service.IIntSetService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 积分设置Controller
 * 
 * @author hanning
 * @date 2021-08-23
 */
@RestController
@RequestMapping("/community/set")
public class IntSetController extends BaseController
{
    @Autowired
    private IIntSetService intSetService;

    @Autowired
    private IIntApplyService applyService;

    /**
     * 查询积分设置列表
     */
    @PreAuthorize("@ss.hasPermi('community:set:list')")
    @GetMapping("/list")
    public TableDataInfo list(IntSet intSet)
    {
        startPage();
        intSet.setCommunityId(SecurityUtils.getCommunityId());
        List<IntSet> list = intSetService.selectIntSetList(intSet);
        return getDataTable(list);
    }

    /**
     * 导出积分设置列表
     */
    @PreAuthorize("@ss.hasPermi('community:set:export')")
    @Log(title = "积分设置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(IntSet intSet)
    {
        List<IntSet> list = intSetService.selectIntSetList(intSet);
        ExcelUtil<IntSet> util = new ExcelUtil<IntSet>(IntSet.class);
        return util.exportExcel(list, "积分设置数据");
    }

    /**
     * 获取积分设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('community:set:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(intSetService.selectIntSetById(id));
    }

    /**
     * 新增积分设置
     */
    @PreAuthorize("@ss.hasPermi('community:set:add')")
    @Log(title = "积分设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IntSet intSet)
    {
        return toAjax(intSetService.insertIntSet(intSet));
    }

    /**
     * 申请修改积分设置
     */
    @PreAuthorize("@ss.hasPermi('community:set:edit')")
    @Log(title = "积分设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IntSet set)
    {
        // 构建申请
        IntApply apply = new IntApply();
        apply.setCommunityId(SecurityUtils.getCommunityId());
        apply.setSetId(set.getId());
        apply.setExpectVal(set.getPrice());
        apply.setExpectEnable(set.getEnable());
        apply.setApplicantId(SecurityUtils.getUserID());
        // 暂时先写死
        apply.setOwners(1);
        return toAjax(applyService.insertIntApply(apply));
    }

    /**
     * 删除积分设置
     */
    @PreAuthorize("@ss.hasPermi('community:set:remove')")
    @Log(title = "积分设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(intSetService.deleteIntSetByIds(ids));
    }
}
